﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

namespace Web.DisplayView
{
    public class ViewFactory
    {
        private static Dictionary<string, Type> _types;

        private static Dictionary<string, Type> FindTypes()
        {
            var types = new Dictionary<string, Type>();

            var asm = Assembly.GetExecutingAssembly();
            foreach (var type in asm.GetTypes().Where( t => t.GetInterface( "IDisplayView" ) != null ))
            {
                var view = (IDisplayView)Activator.CreateInstance( type );
                types.Add( view.Name(), type );
            }

            return types;
        }

        public static Dictionary<string, Type> Types
        {
            get
            {
                return _types ?? (_types = FindTypes());
            }

        }
        public static List<string> Names
        {
            get
            {
                return Types.Select( t => t.Key ).ToList();
            }
        }

        public static IDisplayView Create(string name)
        {            
            return (IDisplayView)Activator.CreateInstance(Types[name]);
        }
    }
}